package com.ruoyi.web.controller.vantController.galleryController;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.domain.GalleryCollection;
import com.ruoyi.system.domain.customer.CustomerLogin;
import com.ruoyi.system.domain.gallery.GalleryComment;
import com.ruoyi.system.domain.gallery.GalleryInfo;
import com.ruoyi.system.domain.zhuangxiumain.shopMain.BrandInfo;
import com.ruoyi.system.service.IGalleryCollectionService;
import com.ruoyi.system.service.vantservice.galleryservice.ApiGalleryService;
import com.ruoyi.system.utils.TokenUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Api(value = "前端图库接口", tags = {"前端图库接口"})
@RestController
@RequestMapping("/api/gallery")
@CrossOrigin
public class ApiGalleryController extends BaseController {

    @Autowired
    private ApiGalleryService apiGalleryService;

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private TokenUtils tokenUtils;

    @Autowired
    private IGalleryCollectionService galleryCollectionService;

    @ApiOperation(value = "查询图库列表")
    @GetMapping("/selectGalleryList/{galleryInfoType}")
    public AjaxResult selectGalleryList(@PathVariable(value = "galleryInfoType", required = false) String galleryInfoType){
        return AjaxResult.success(apiGalleryService.selectGalleryInfoListByType(galleryInfoType));
    }

    @GetMapping(value = "/selectGalleryInfoListByTypeOne")
    public TableDataInfo list(GalleryInfo galleryInfo){
        CustomerLogin customerLogin = tokenUtils.getLoginCustomer(request);

        List<GalleryInfo> galleryInfos = new ArrayList<>();

        List<GalleryInfo> list = apiGalleryService.selectGalleryInfoListByTypeOne(galleryInfo);
        for (GalleryInfo galleryInfo1 : list){
            GalleryCollection galleryCollection = new GalleryCollection();
            galleryCollection.setGalleryInfoId(galleryInfo1.getGalleryInfoId());
            customerLogin.setCustomerId(customerLogin.getCustomerId());
            List<GalleryCollection> galleryCollections = galleryCollectionService.selectGalleryCollectionList(galleryCollection);
            if(galleryCollections.size()>0){
                galleryInfo1.setCourseType("1");
            }else {
                galleryInfo1.setCourseType("0");
            }
            galleryInfos.add(galleryInfo1);
        }
        return getDataTable(galleryInfos);
    }

    @GetMapping(value = "/selectGalleryInfoListByTypeTwo")
    public TableDataInfo listTwo(GalleryInfo galleryInfo){
        CustomerLogin customerLogin = tokenUtils.getLoginCustomer(request);

        List<GalleryInfo> galleryInfos = new ArrayList<>();

        List<GalleryInfo> list = apiGalleryService.selectGalleryInfoListByTypeTwo(galleryInfo);
        for (GalleryInfo galleryInfo1 : list){
            GalleryCollection galleryCollection = new GalleryCollection();
            galleryCollection.setGalleryInfoId(galleryInfo1.getGalleryInfoId());
            customerLogin.setCustomerId(customerLogin.getCustomerId());
            List<GalleryCollection> galleryCollections = galleryCollectionService.selectGalleryCollectionList(galleryCollection);
            if(galleryCollections.size()>0){
                galleryInfo1.setCourseType("1");
            }else {
                galleryInfo1.setCourseType("0");
            }
            galleryInfos.add(galleryInfo1);
        }
        return getDataTable(galleryInfos);
    }


    @GetMapping(value = "/selectGalleryInfoListByTypeThree")
    public TableDataInfo listThree(GalleryInfo galleryInfo){
        CustomerLogin customerLogin = tokenUtils.getLoginCustomer(request);

        List<GalleryInfo> galleryInfos = new ArrayList<>();

        galleryInfo.setCustomerId(customerLogin.getCustomerId());

        List<GalleryInfo> list = apiGalleryService.selectGalleryInfoListByTypeThree(galleryInfo);
        for (GalleryInfo galleryInfo1 : list){
            GalleryCollection galleryCollection = new GalleryCollection();
            galleryCollection.setGalleryInfoId(galleryInfo1.getGalleryInfoId());
            customerLogin.setCustomerId(customerLogin.getCustomerId());
            List<GalleryCollection> galleryCollections = galleryCollectionService.selectGalleryCollectionList(galleryCollection);
            if(galleryCollections.size()>0){
                galleryInfo1.setCourseType("1");
            }else {
                galleryInfo1.setCourseType("0");
            }
            galleryInfos.add(galleryInfo1);
        }
        return getDataTable(galleryInfos);
    }



    @ApiOperation(value = "根据ID查询图库信息")
    @GetMapping("/selectGalleryById/{galleryInfoId}")
    public AjaxResult selectGalleryById(@PathVariable(value = "galleryInfoId", required = false) Long galleryInfoId){
        return AjaxResult.success(apiGalleryService.selectGalleryInfoById(galleryInfoId));
    }

    @ApiOperation(value = "查询案例说明列表")
    @GetMapping("/selectGalleryCaseList/{galleryOriginalId}")
    public AjaxResult selectGalleryCaseList(@PathVariable(value = "galleryOriginalId", required = false) Long galleryOriginalId){
        return AjaxResult.success(apiGalleryService.selectGalleryCasesList(galleryOriginalId));
    }

    @ApiOperation(value = "根据ID查询图库信息评论")
    @GetMapping("/selectGalleryComment/{galleryInfoId}")
    public AjaxResult selectGalleryComment(@PathVariable(value = "galleryInfoId", required = false) Long galleryInfoId){
        return AjaxResult.success(apiGalleryService.selectGalleryComment(galleryInfoId));
    }

    @ApiOperation(value = "添加图库评论信息")
    @PostMapping("/addGalleryComment")
    public AjaxResult addGalleryComment(@Validated @RequestBody GalleryComment galleryComment){
        // 获取前台登录用户信息
        CustomerLogin customerLogin = tokenUtils.getLoginCustomer(request);
        galleryComment.setGalleryCommentCust(customerLogin.getCustomerId());
        return AjaxResult.success(apiGalleryService.addGalleryComment(galleryComment));
    }
}
